package com.lc.afbx.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.lc.afbx.BaseApplication;
import com.lc.afbx.R;
import com.lc.afbx.adapters.XuBaoAdapter;
import com.lc.afbx.conn.GetApiConfirmXuBao;
import com.lc.afbx.util.PrefrenceConfig;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/22.
 */
public class RenewalActivity extends BaseActivity implements View.OnClickListener {

    private XuBaoAdapter adapter;
    private List<GetApiConfirmXuBao.Data> lists = new ArrayList<>();
    private ListView renewal_lv;

    private String type;
    private int selectPosition = -1;

    private GetApiConfirmXuBao getApiConfirmXuBao = new GetApiConfirmXuBao(new AsyCallBack<GetApiConfirmXuBao.Info>() {

        @Override
        public void onSuccess(String toast, int type, GetApiConfirmXuBao.Info info) throws Exception {

            lists.addAll(info.dataList);

            renewal_lv.setAdapter(adapter = new XuBaoAdapter(context, lists));

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(context, toast);

        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_renewal);

        renewal_lv = ((ListView) findViewById(R.id.renewal_lv));
        renewal_lv.setDivider(null);


        renewal_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (adapter.colorMap.get(position) == getResources().getColor(R.color.white)) {
                    adapter.init();
                    selectPosition = position;
                    adapter.colorMap.put(position, getResources().getColor(R.color.selectView));
                } else {
                    selectPosition = -1;
                    adapter.colorMap.put(position, getResources().getColor(R.color.white));
                }
                adapter.notifyDataSetChanged();


            }
        });


        String flag = getIntent().getStringExtra(PrefrenceConfig.INTENT_TAG);

        if (flag.equals("CompulsoryInsuranceActivity")) {
            type = PrefrenceConfig.ONE;
            getApiConfirmXuBao.type = PrefrenceConfig.ONE;
        } else {
            type = PrefrenceConfig.TWO;
            getApiConfirmXuBao.type = PrefrenceConfig.TWO;
        }

        getApiConfirmXuBao.userid = BaseApplication.BasePreferences.readUserId();
        getApiConfirmXuBao.execute(context);

    }


    @Override
    public void onClick(View view) {
        super.onClick(view);

        switch (view.getId()) {
            case R.id.back:
                finish();
                return;

            case R.id.renewal_bt:
                Intent intent = new Intent(context, RenewalApplyActivity.class);
//                UtilLog.e("这个是虚报的值", dataList.get(position).id);

                if (selectPosition < 0) {
                    UtilToast.show(context, "请选择一个投保车辆");
                } else {
                    intent.putExtra(PrefrenceConfig.INTENT_TAG, lists.get(selectPosition).id);
                    intent.putExtra("type", type);//这个type是区分是 交强险的续保 还是 商业险的续保
                    startActivity(intent);
                }
                break;

        }
    }
}
